package com.blgroup.oa.configuration;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.InputStream;
import java.util.UUID;


@WebServlet(urlPatterns = "/initData")
public class InitServlet extends HttpServlet {
    public InitServlet() {
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        ServletContext servletContext = servletConfig.getServletContext();
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        IdentityService identityService = (IdentityService) webApplicationContext.getBean("identityService");
        this.initGroupsAndUsers(identityService);
        RepositoryService repositoryService = (RepositoryService) webApplicationContext.getBean("repositoryService");
        this.initProcessDefinition(repositoryService);
    }

    private void initProcessDefinition(RepositoryService repositoryService) {
        InputStream is1 = InitServlet.class.getResourceAsStream("/bpmn/CountSalary.xml");
        InputStream is2 = InitServlet.class.getResourceAsStream("/bpmn/ExpenseAccount.xml");
        InputStream is3 = InitServlet.class.getResourceAsStream("/bpmn/SalaryAdjust.bpmn");
        InputStream is4 = InitServlet.class.getResourceAsStream("/bpmn/Vacation.bpmn");
        repositoryService.createDeployment().addInputStream("/bpmn/CountSalary.xml", is1).addInputStream("/bpmn/ExpenseAccount.xml", is2).addInputStream("/bpmn/SalaryAdjust.bpmn", is3).addInputStream("/bpmn/Vacation.bpmn", is4).deploy();
    }

    private void createGroup(IdentityService identityService, String groupId, String groupName, String groupType, String userId, String userName, String passwd) {
        Group g1 = identityService.newGroup(groupId);
        g1.setName(groupName);
        g1.setType(groupType);
        identityService.saveGroup(g1);
        User u = identityService.newUser(userId);
        u.setLastName(userName);
        u.setPassword(passwd);
        identityService.saveUser(u);
        identityService.setUserInfo(u.getId(), "age", String.valueOf(30));
        identityService.createMembership(u.getId(), g1.getId());
    }

    private void initGroupsAndUsers(IdentityService identityService) {
        this.createGroup(identityService, "employee", "员工组", "employee", UUID.randomUUID().toString(), "员工甲", "123456");
        this.createGroup(identityService, "manager", "经理组", "manager", UUID.randomUUID().toString(), "经理甲 ", "123456");
        this.createGroup(identityService, "director", "总监组", "director", UUID.randomUUID().toString(), "总监甲 ", "123456");
        this.createGroup(identityService, "hr", "人事组", "hr", UUID.randomUUID().toString(), "人事甲 ", "123456");
        this.createGroup(identityService, "boss", "老板组", "boss", UUID.randomUUID().toString(), "老板甲 ", "123456");
        this.createGroup(identityService, "finance", "财务组", "finance", UUID.randomUUID().toString(), "财务甲 ", "123456");
    }
}